package com.hlkj.baojin.controller;

import com.hlkj.baojin.entity.DeviceErrLog;
import com.hlkj.baojin.entity.dto.Details;
import com.hlkj.baojin.entity.dto.DeviceDto;
import com.hlkj.baojin.entity.dto.DeviceSearchDto;
import com.hlkj.baojin.service.DeviceService;
import com.hlkj.baojin.utils.ValidationUtil;
import com.hlkj.baojin.utils.bean.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author HULIN
 * @created with: baojin
 * @package com.hlkj.baojin.controller
 * @description:
 * @date 2018/12/17 15:40
 * @modified By:
 * @Copyright © 2018 HAILIANG Info. Tech Ltd. All rights reserved.
 */
@Api(value = "/bjjk", description = "管理")
@RestController
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "设备列表", tags = {"设备管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = DeviceDto.class))
    @PostMapping(value = "/device/list")
    public ResponseEntity<JsonResult> deviceList(@RequestBody DeviceSearchDto deviceSearchDto) {
        return ResponseEntity.ok(JsonResult.success(deviceService.findDevice(deviceSearchDto)));
    }

    @ApiOperation(value = "设备保存", tags = {"设备管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = void.class))
    @PostMapping(value = "/device/save")
    public ResponseEntity<JsonResult> deviceSave(@RequestBody DeviceDto deviceDto) {
        ValidationUtil.validate(deviceDto);
        deviceService.saveDevice(deviceDto);
        return ResponseEntity.ok(JsonResult.success());
    }

    @ApiOperation(value = "设备更新", tags = {"设备管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = void.class))
    @PostMapping(value = "/device/update")
    public ResponseEntity<JsonResult> deviceUpdate(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDevice(deviceDto);
        return ResponseEntity.ok(JsonResult.success());
    }

    @ApiOperation(value = "设备删除", tags = {"设备管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = void.class))
    @PostMapping(value = "/device/delete/{deviceId}")
    public ResponseEntity<JsonResult> deviceDelete(@PathVariable("deviceId") String deviceId) {
        deviceService.deleteDevice(deviceId);
        return ResponseEntity.ok(JsonResult.success());
    }

    @ApiOperation(value = "设备查看", tags = {"设备管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = DeviceDto.class))
    @GetMapping(value = "/device/get/{deviceId}")
    public ResponseEntity<JsonResult> deviceGet(@PathVariable("deviceId") String deviceId) {
        return ResponseEntity.ok(JsonResult.success(deviceService.findDeviceById(deviceId)));
    }

    @ApiOperation(value = "故障记录", tags = {"设备管理"})
    @ApiResponses(value = @ApiResponse(code = 200, message = "成功", response = Details.class))
    @GetMapping(value = "/device/devicErrLog/{deviceId}")
    public ResponseEntity<JsonResult> devicErrLogGet(@PathVariable("deviceId") String deviceId) {
        return ResponseEntity.ok(JsonResult.success(deviceService.getError(deviceId)));
    }
}
